﻿using AutoMapper;
using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.ReadApi.Command;
using U.B.ReadApi.Dto;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander
{
    public class DepartmentUserCommandHander : IRequestHandler<DepartmentUserCommand, ApiResult<List<DepartmentUserDto>>>
    {
        public readonly IRepistory<User> userrepistory;
        public readonly IMapper mapper;

        public DepartmentUserCommandHander(IRepistory<User> userrepistory, IMapper mapper)
        {
            this.userrepistory = userrepistory;
            this.mapper = mapper;
        }
        /// <summary>
        /// 查询部门
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<DepartmentUserDto>>> Handle(DepartmentUserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<DepartmentUserDto>> apiResult = new ApiResult<List<DepartmentUserDto>>();
            var result = userrepistory.GetAll().Where(x => x.DepartmentId == request.DepartmentId);
            apiResult.code = Renum.显示成功;
            apiResult.Message = Renum.显示成功.ToString();
            apiResult.Data = mapper.Map<List<DepartmentUserDto>>(result);
            return Task.FromResult(apiResult);

        }
    }
}
